<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="配置表" @del="del" @newCreate="newCreate" @configcolumn="configcolumn"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :apiParams="apiParams" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'配置表'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle';

export default {
  name: 'tableconfig',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null
      },
      options:{
        dynamicsClass:[],
        parentnode:[],
        updRule:[],
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        },
        {
          label: "字段配置",
          disabled: true,
          invalid: 'configcolumn'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '表名称',
          prop: 'name',
          input: 'input'
        },
         {
          label: '父节点',
          prop: 'parentnode',
          input: 'select',
          options:this.options.parentnode
        },
        {
          label: '对应报文根节点',
          prop: 'requestrootnode',
          input: 'input'
        },
        {
          label: '更新策略',
          prop: 'updaterule',
          input: 'input'
        },
        {
          label: '是否作为作为报文节点',
          prop: 'activerequest',
          input: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '表名称',
          prop: 'name',
          type: 'input'
        },
        {
          label: '父节点',
          prop: 'parentnode',
          type: 'select',
          options:this.options.parentnode,
        },
        {
          label: '对应报文根节点',
          prop: 'requestrootnode',
          type: 'input'
        },
        {
          label: '更新规则',
          prop: 'updaterule',
          type: 'select',
          options:this.options.updRule
        },
        {
          label: '是否作为作为报文节点',
          prop: 'activerequest',
          type: 'select',
          options: [
          { label: '是', value: 'true' },
          { label: '否', value: 'false' }
          ]
        }
      ]
    },
    formRules() {
      return {
        activeclass: [
          {
            required: true,
            message: '请选择表名称',
            trigger: 'change'
          }
        ],
      }
    },
    apiMap() {
      return {
        getListApi: '/v1/ediTableConfig/selectEdiTableConfigList',
        getInfoApi:'/v1/ediTableConfig/selectEdiTableConfigById',
        editApi:'/v1/ediTableConfig/editEdiTableConfig',
        deleteApi: '/v1/ediTableConfig/deleteEdiTableConfig',
        addApi: '/v1/ediTableConfig/addEdiTableConfig'
      }
    },
    apiParams() {
      return { apiid: this.$route.query.id }
    }
  },
  methods: {
  async configcolumn(data){
      if(data.length != 1){
        return this.$message.error("请选择一条数据进行操作")
      }
      // await this.$post('/v1/ediColumnValueConfig/initColumnsValueConfig',{editableconfigid:data[0].id}).then(res => { })
      this.$router.push({ path: '/app/ediapi/columnValueConfig', query: { id: data[0].id } })
    },
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        params.apiid = this.$route.query.id;
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          if(res.returnCode == 400){
            this.$message.error(res.returnMgr)
          }else{
            this.getTableData()
            this.$refs.detail?.resetFields()
            this.dialogModule.flag = false
          }

        })
      })

    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, { 
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.name}]字段配置`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增流水号规则'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
      this.$post('/v1/ediTableConfig/queryParentNode',{apiid:this.$route.query.id}).then(res => {
        this.options.parentnode = res.data.item;
      });
       this.$post('/v1/ediApi/queryUpdRule',{}).then(res => {
        this.options.updRule = res.data.item;
      })
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
